/*
*  Copyright 2019-2023 CY
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.ejweixin.service.impl;

import me.zhengjie.modules.ejweixin.domain.NgUserCollect;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.ejweixin.service.NgUserCollectService;
import me.zhengjie.modules.ejweixin.domain.vo.NgUserCollectQueryCriteria;
import me.zhengjie.modules.ejweixin.mapper.NgUserCollectMapper;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author cy
* @date 2025-04-29
**/
@Service
@RequiredArgsConstructor
public class NgUserCollectServiceImpl extends ServiceImpl<NgUserCollectMapper, NgUserCollect> implements NgUserCollectService {

    private final NgUserCollectMapper ngUserCollectMapper;


    @Override
    public PageResult<NgUserCollect> queryAll(NgUserCollectQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(ngUserCollectMapper.findAll(criteria, page));
    }

    @Override
    public List<NgUserCollect> queryAll(NgUserCollectQueryCriteria criteria){
        return ngUserCollectMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(NgUserCollect resources) {
        UserDetails loginUser = SecurityUtils.getCurrentUser();
        Long userId = ngUserCollectMapper.getUserIdByName(loginUser.getUsername());
        resources.setUserId(userId.intValue());
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NgUserCollect resources) {
        NgUserCollect ngUserCollect = getById(resources.getId());
        ngUserCollect.copy(resources);
        saveOrUpdate(ngUserCollect);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids, String userName) {
        Long userId = ngUserCollectMapper.getUserIdByName(userName);
        for(Integer id:ids){
            ngUserCollectMapper.deleteByUserIdAndDataId(userId.intValue(), id);
        }
    }

    @Override
    public void download(List<NgUserCollect> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (NgUserCollect ngUserCollect : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("标记类型 1团队 2案例  3工地", ngUserCollect.getTagType());
            map.put("用户ID", ngUserCollect.getUserId());
            map.put("数据主键", ngUserCollect.getDataId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void deleteByUserIdAndDataIdAndTagType(String tagType, Long userId, Integer dateId) {
        ngUserCollectMapper.deleteByUserIdAndDataIdAndTagType(userId, dateId, tagType);
    }
}